package net.lahuobao.finance.service.impl.userManagement;



import java.math.BigDecimal;
import java.util.*;

import org.hibernate.property.IndexPropertyAccessor.IndexGetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.finance.dao.ScoreMapper;
import net.lahuobao.finance.dao.UserMapper;
import net.lahuobao.finance.entity.depart.EValidateState;
import net.lahuobao.finance.entity.depart.User;
import net.lahuobao.finance.entity.depart.UserAction;
import net.lahuobao.finance.entity.organizationEntity.OrganizationEntity;
import net.lahuobao.finance.entity.score.EScoreType;
import net.lahuobao.finance.entity.score.Score;
import net.lahuobao.finance.service.userManagement.IUserService;

/**
 * @author Frank
 * @version 1.0
 * @created 26-Jan-2015 3:18:58 PM
 */
@Service("userManagementService")
@Transactional
public class UserManagementServiceImpl implements IUserService {
    @Autowired
    private UserMapper userDao;
    @Autowired
    private ScoreMapper scoreDao;
    public User selectUserById(int userId) {
        User user = userDao.selectByUserId(userId);
        return user;
    }
    /**
     * @param user
     */
    public void updateUser(User user) {
    	Long userId  = user.getUserId();
    	
        User originalUser = userDao.selectByPrimaryKey(userId.intValue());
        
        if (originalUser.getValidateState() == EValidateState.VALIDATED.getValue() && user.getValidateState() == EValidateState.WAIT_VALIDATE.getValue()) {
            return;
        }
        userDao.updateByPrimaryKey(user);
      
    }
    public void approveUser(Integer userId) {
        User user = this.selectUserById(userId);
        user.setValidateState(EValidateState.VALIDATED.getValue());
        user.setValidateDatetime(new Date());
        this.updateUser(user);
               // 注册积分
        obtainScore(userId, EScoreType.REGISTER);
    }
    
    private void obtainScore(Integer userId, EScoreType type) {
        // 获取当前积分
        Integer currentScore = this.getCurrentScore(userId);
        Integer total = currentScore + type.getScore();

        // 得分时间
        Date changeTime = new Date();

        Score score = new Score();
        score.setUserId(userId);
        score.setChangeType(type.getChangeType());
        score.setChangeComments(type.getComments());
        score.setScore(type.getScore());
        score.setChangeTime(changeTime);
        score.setTotal(total);
        scoreDao.insert(score);
    }
    
    
    private int getCurrentScore(Integer userId) {
        ArrayList<Score> scoreList = scoreDao.selectListByUserId(userId);
        if (scoreList.size() == 0) {
            return 0;
        } else {
            Score score = scoreList.get(scoreList.size() - 1);
            int total = score.getTotal();
            return total;
        }
    }
    public void denyUser(Integer userId) {
        User user = this.selectUserById(userId);
        user.setValidateState(EValidateState.DENY.getValue());
        this.updateUser(user);

    }
}